#!/usr/bin/python
from pottononline.map.models import Icon, MarkerType
import os, Image
imgdir = os.path.join(os.path.split(__file__)[0], "..", "media", "map", "icons")
files = os.listdir(imgdir)
shadows = {}

for i in Icon.objects.all():
  print "Deleting icon id:" + str(i.id)
  i.delete()

for shadow in [x for x in files if x.find("shadow") != -1]:
  if shadow.split('.')[-1] == 'png':
    print "Doing shadow icon",shadow
    icon = Icon()
    icon.image = shadow
    img = Image.open(os.path.join(imgdir, shadow))
    icon.width, icon.height = img.size
    icon.save()
    shadows[shadow] = icon

for main in [x for x in files if x.find("shadow") == -1]:
  if main.split('.')[-1] == 'png':  
    print "Doing icon",main
    icon = Icon()
    icon.image = main
    img = Image.open(os.path.join(imgdir, main))
    icon.width, icon.height = img.size
    fn, fe = os.path.splitext(main)
    shadow_name = ''.join([fn, ".shadow", fe])
    icon.shadow = shadows.get(shadow_name, None)
    icon.save()
  
#  Hardcoded Marker Types (for now): Friendly Name :vs: icons file name without the extenstion

markertypes = {
'Arts':'arts', 'Bar':'bar', 'Geophoto':'camera', 'Camping':'campground',
'Cafe':'coffeehouse', 'Supermarket':'convienancestore', 'Cycle Route':'cycling',
'Golf Course':'golfer', 'New Marker':'green', 'Grocery Shop':'grocerystore', 'Walking Route':'hiker',
'Horse Riding':'horsebackriding', 'Information':'info', 'Lodging':'lodging', 'Petrol':'petrol',
'Public Phone':'phone', 'Picnic Area':'picnic', 'Post Office':'postoffice-us', 'Pub':'pub',
'House for sale':'realestate', 'Recycling Area':'recycle', 'Food':'restaurant', 'Hair Dressers':'salon',
'Shop':'shopping', 'Sports Venue':'sportvenue', 'Take Away':'takeaway', 'Public Toilets':'toilets', 'Nature Spot':'tree',
'Special Event':'ylw-pushpin', 'Car Park':'parkinglot', 'Bus Stop':'bus', 'Business':'company', 'Pub Food':'pubfood',
'School':'school', 'Place of worship':'pow', 'Fire Station':'firedept','Movies':'movies','Child Care':'childcare'
}

for each in markertypes:
  print "Creating MarkerType:  " + each
  marker_type = MarkerType()
  marker_type.name = each
  marker_type.icon = Icon.objects.filter(image=markertypes[each]+fe)[0]
  marker_type.save()

